Cars Dataset Exploration with plotly.py version 3.0

In [1]:
import pandas as pd
import numpy as np

import plotly.graph_objs as go
import plotly.offline as py

from ipywidgets import interactive, HBox, VBox
py.init_notebook_mode()

import warnings
warnings.filterwarnings('ignore')

Load cars dataset

In [11]:
cars_df = pd.read_csv('https://raw.githubusercontent.com/jonmmease/plotly_ipywidget_notebooks/master/notebooks/data/cars/cars.csv')
In [14]:
cars_df.shape
Out[14]:
(5076, 18)
In [15]:
f = go.FigureWidget([go.Scatter(y = cars_df['City mpg'], x = cars_df['City mpg'], mode = 'markers')])
scatter = f.data[0]
N = len(cars_df)
scatter.x = scatter.x + np.random.rand(N)/10 *(cars_df['City mpg'].max() - cars_df['City mpg'].min())
scatter.y = scatter.y + np.random.rand(N)/10 *(cars_df['City mpg'].max() - cars_df['City mpg'].min())
scatter.marker.opacity = 0.5

def update_axes(xaxis, yaxis):
    scatter = f.data[0]
    scatter.x = cars_df[xaxis]
    scatter.y = cars_df[yaxis]
    with f.batch_update():
        f.layout.xaxis.title = xaxis
        f.layout.yaxis.title = yaxis
        scatter.x = scatter.x + np.random.rand(N)/10 *(cars_df[xaxis].max() - cars_df[xaxis].min())
        scatter.y = scatter.y + np.random.rand(N)/10 *(cars_df[yaxis].max() - cars_df[yaxis].min())
    
axis_dropdowns = interactive(update_axes, yaxis = cars_df.select_dtypes('int64').columns, xaxis = cars_df.select_dtypes('int64').columns)
In [16]:
# Create a table FigureWidget that updates on selection from points in the scatter plot of f
t = go.FigureWidget([go.Table(
    header=dict(values=['ID','Classification','Driveline','Hybrid'],
                fill = dict(color='#C2D4FF'),
                align = ['left'] * 5),
    cells=dict(values=[cars_df[col] for col in ['ID','Classification','Driveline','Hybrid']],
               fill = dict(color='#F5F8FF'),
               align = ['left'] * 5))])

def selection_fn(trace,points,selector):
    t.data[0].cells.values = [cars_df.loc[points.point_inds][col] for col in ['ID','Classification','Driveline','Hybrid']]

scatter.on_selection(selection_fn)
In [17]:
# Put everything together
VBox((HBox(axis_dropdowns.children),f,t))